package com.iwith.dongmoon.activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.iwith.dongmoon.gsonlist.MemberList;
import com.iwith.dongmoon.util.ApiCode;
import com.iwith.dongmoon.util.ImageDownloader;
import com.iwith.dongmoon.util.ImageDownloader.Mode;
import com.iwith.dongmoon.util.RequestGson;
import com.iwith.dongmoon.util.ResultCode;
import com.iwith.dongmoon.util.SessionManager;
import com.iwith.dongmoon.util.ThreadInterface;
import com.iwith.dongmoon.util.ThreadTask;

public class Tab_InfoActivity extends Activity implements OnClickListener {
	private SessionManager mSessionManager=null;
	
	private RequestGson requestGson = new RequestGson();
	private TextView name;
	private TextView graduation;
	private TextView nickname;
	private TextView email;
	private TextView address;
	private TextView comment;
	private TextView bizType;
	private TextView bizName;
	private TextView bizPhone;
	private TextView bizAddress;
	private TextView bizInfo;
	private Button modifyBt;
	private Button quitBt;
	private Button logoutBt;

	private ImageView[] bizPhoto;
	private ImageView userPhoto;
	private ImageDownloader mImageDownloader;;

	private Integer[] bizPhotoResource = { R.id.tabinfo_bizphoto1,	R.id.tabinfo_bizphoto2, R.id.tabinfo_bizphoto3 };
	
	private String userid;
	private Context mContext;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.tab_info);
		// TODO Auto-generated method stub
		mSessionManager=new SessionManager(this);
	}

	private void Initialize() {
		mContext = this;		
		userid = mSessionManager.getUserid();
		
		bizPhoto = new ImageView[3];
		for (int i = 0; i < 3; i++) {
			bizPhoto[i] = (ImageView) findViewById(bizPhotoResource[i]);
		}

		name = (TextView) findViewById(R.id.tabinfo_name);
		graduation = (TextView) findViewById(R.id.tabinfo_graduation);
		nickname = (TextView) findViewById(R.id.tabinfo_nickname);
		email = (TextView) findViewById(R.id.tabinfo_email);
		address = (TextView) findViewById(R.id.tabinfo_address);
		comment = (TextView) findViewById(R.id.tabinfo_message);
		bizType = (TextView) findViewById(R.id.tabinfo_jobtypename);
		bizName = (TextView) findViewById(R.id.tabinfo_jobname);
		bizPhone = (TextView) findViewById(R.id.tabinfo_jobphone);
		bizAddress = (TextView) findViewById(R.id.tabinfo_jobaddress);
		bizInfo = (TextView) findViewById(R.id.tabinfo_jobmessage);
		userPhoto = (ImageView) findViewById(R.id.tabinfo_userphoto);
		modifyBt = (Button) findViewById(R.id.tabinfo_modifybt);
		quitBt = (Button) findViewById(R.id.tabinfo_leavebt);
		logoutBt = (Button) findViewById(R.id.tabinfo_logoutbt);

		mImageDownloader = new ImageDownloader();
		mImageDownloader.setMode(Mode.CORRECT);

		setData(ApiCode.USERINFO);

		userPhoto.setOnClickListener(this);
		for(int i=0; i<3; i++){
			bizPhoto[i].setOnClickListener(this);
		}
					
		logoutBt.setOnClickListener(this);
		modifyBt.setOnClickListener(this);
		quitBt.setOnClickListener(this);
	}

	@Override
	public void onResume(){
		super.onResume();
		ThreadTask.onFinishAll();
		Initialize();
	}
	
	@Override
	public void onPause(){
		super.onPause();
		ThreadTask.onFinishAll();
	}

	private void setData(final String code) {
		ThreadInterface threadInterface = new ThreadInterface() {
			MemberList memberList;

			public void onThread() {
				memberList = requestGson.getMemberGsonBySendingAPI(mContext, ApiCode.API_USER, "GET", "/" + userid);
			}

			public void onThreadEnd() {
				// TODO NOTE : set member info
				if (memberList.getResultCode().equals(ResultCode.SUCCESS) == true) {
					name.setText(memberList.getContent().getUsername());
					graduation.setText(memberList.getContent().getGraduation()
							.toString());
					nickname.setText(memberList.getContent().getNickname());
					email.setText(memberList.getContent().getEmail());
					address.setText(memberList.getContent().getAddress());
					comment.setText(memberList.getContent().getComment());
					bizType.setText(memberList.getContent().getBizType());
					bizName.setText(memberList.getContent().getBizName());
					bizPhone.setText(memberList.getContent().getBizPhone());
					bizAddress.setText(memberList.getContent().getBizAddress());
					bizInfo.setText(memberList.getContent().getBizInfo());
					
					if (memberList.getContent().getThumbnail() != null) {
						mImageDownloader.download(memberList.getContent()
								.getThumbnail(), userPhoto);
					}

					String[] bizThumnails = new String[3];
					bizThumnails[0] = memberList.getContent().getBizThumbnail1();
					bizThumnails[1] = memberList.getContent().getBizThumbnail2();
					bizThumnails[2] = memberList.getContent().getBizThumbnail3();

					for (int i = 0; i < 3; i++) {
						if (bizThumnails[i] != null) {
							mImageDownloader.download(bizThumnails[i], bizPhoto[i]);
						}
					}
					
				} else {
					requestGson.alertToast(mContext, code + " 데이터 로딩 실패");
				}
			}
		};
		ThreadTask task = new ThreadTask((Activity)mContext, threadInterface);
		task.execute();
	}

	public void onClick(View v) {
		// TODO Auto-generated method stub
		if(v.getId()==userPhoto.getId()){
			Intent intent=new Intent(mContext, PhotosActivity.class);
			intent.putExtra("userid", userid);
			intent.putExtra("code", ApiCode.USERPHOTO);
			startActivity(intent);
		} else if(v.getId()==logoutBt.getId()) {
			mSessionManager.logout();
			Intent intent = new Intent(mContext, MainIconView.class);
			intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
			startActivity(intent);
		} else if(v.getId()==modifyBt.getId()){
			Intent intent = new Intent(mContext, PersonalInfoModifyActivity.class);
			startActivity(intent);
		} else if(v.getId()==quitBt.getId()){
			finish();
		} else {
			for(int i=0; i<3; i++){
				if(v.getId()==bizPhoto[i].getId()){
					Intent intent=new Intent(mContext, PhotosActivity.class);
					intent.putExtra("userid", userid);
					intent.putExtra("code", ApiCode.BIZPHOTO);
					startActivity(intent);
				}
			}
		}	
	}
}
